﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace 获取项目信息
{
    [Transaction(TransactionMode.Manual)]
    public class ExamineFamily : IExternalCommand
    {
        Family family = null;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            string family_json = ExactFamily(commandData);
            TaskDialog.Show("Revit", family_json);
            return Result.Succeeded;

        }

        public  string ExactFamily(ExternalCommandData commandData, string file_path)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            Transaction tran = new Transaction(doc);
            tran.Start("载入族");
            bool b = doc.LoadFamily(file_path, out family);
            tran.Commit();
            if (b) { TaskDialog.Show("11", "加载成功"); }
            //Get_FamilySymbol(family);
            Element element = family as Element;
            //getSymbolpara();//获取族类型参数
            //ShowAllBuildinPara();//将族类型信息参数转成table表格


            string family_json = "{ \" Family \": " + "\"" + family.Name + "\",\n   \" symbols \":[";
            try
            {
                // 获得族管理器
                FamilyManager familyMgr = doc.FamilyManager;
                FamilyType currType = familyMgr.CurrentType;
                if (currType != null)
                {   // 当前族类型可能为空，故需进行空检查
                    FamilyParameterSet familyParamSet = familyMgr.Parameters;
                    ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
                    foreach (ElementId id in familySymbolIds)
                    {
                        FamilySymbol familySymbol = family.Document.GetElement(id) as FamilySymbol;
                        family_json += "{ \" name \": " + "\"" + familySymbol.Name + "\",";
                        foreach (FamilyParameter param in familyParamSet)
                        {

                            if (param.Definition.ParameterType == ParameterType.Length)//可找到对应参数值
                            {
                                double oneValueOfFamilySymbol = currType.AsDouble(param).Value;
                                double unitByMeter = UnitUtils.Convert(oneValueOfFamilySymbol, DisplayUnitType.DUT_DECIMAL_FEET, DisplayUnitType.DUT_METERS);
                                family_json += "\"" + param.Definition.Name + "\" " + ":\"" + unitByMeter + "\" ,";
                            }
                            else//找不到对应参数值
                            {
                                family_json += "\"" + param.Definition.Name + "\":\"" + " - " + "\",";
                            }
                        }
                        family_json += "\"" + "" + "\":\"" + " " + "\"},";

                        //BsonDocument document1 = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(family_json);
                        //StreamWriter sw = new StreamWriter("F:\\1.txt", false);
                        //sw.WriteLine(family_json);
                        //sw.Close();
                        //TaskDialog.Show("Result", "保存成功");
                        //MessageBox.Show(family_json, "信息");

                    }
                    family_json += "{}]}";
                }
            }
            catch (Exception ex)
            {
                //TaskDialog.Show("Revit", ex.Message);
                //throw;
            }
            // string json_format_string = Newtonsoft.Json.JsonConvert.SerializeObject(family_json);
            //弹窗展示json格式参数信息
            //TaskDialog.Show("Result", json_format_string);

            //测试用下载txt格式文件
            //StreamWriter sw = new StreamWriter("D:\\1.txt", false);
            //sw.WriteLine(json_format_string);
            //sw.Close();
            //TaskDialog.Show("Result", "保存成功");
            //MessageBox.Show(info, "信息");
            return family_json;
            //return Result.Succeeded;
        }
    }
}